package logic

import (
	"context"
	"errors"
	"practice-one/models/mysql"

	"practice-one/userrpc/internal/svc"
	"practice-one/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDelLogic {
	return &CollectDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDelLogic) CollectDel(in *userrpc.CollectDelRequest) (*userrpc.CollectDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var goods mysql.Goods
	if err := goods.FindGoodsById(mysql.DB, in.GoodsId); err != nil {
		return nil, errors.New("商品不存在")
	}

	var collect mysql.Collect
	if err := collect.CollectFind(mysql.DB, in.UserId, in.GoodsId); err != nil {
		return nil, err
	}
	if collect.ID == 0 {
		return nil, errors.New("未收藏")
	}

	err := collect.DelCollect(mysql.DB, collect.ID)
	if err != nil {
		return nil, errors.New("取消收藏失败")
	}

	return &userrpc.CollectDelResponse{}, nil
}
